package com.cofluent.web.client;

import com.cofluent.web.client.model.Build;
import com.cofluent.web.client.widget.BuildSummary;
import com.cofluent.web.client.widget.DynaTableWidget;
import com.cofluent.web.client.widget.BuildSummary.BuildSummaryViewer;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BuildInformation implements EntryPoint {

  private static final String[] STYLES = new String [] {"cvs", "version", "build", "release", "data", "systemc", "generator", "manual", "compiler", "jdk"};
  private static final String[] COLUMN_NAMES = new String [] {"CVS Tag", "Version #", "Build #", "Release #", "Data #", "Systemc Simulation Library #", "Meta Generator #", "User's Manual/Guide", "GNU Compiler", "J2SDK"};

  private HTML createSummaryHeader(String title)
  {
	  return new HTML (" * <i>" + title + "</i>");
  }

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
	  TabPanel tabPanel=new TabPanel();
	  VerticalPanel vpanel=new VerticalPanel();
	  HorizontalPanel hpanel= new HorizontalPanel();
	  hpanel.add(new Label("Search : "));
	  hpanel.add(new TextBox());
	  vpanel.add(hpanel);
	  final BuildInformationProvider buildInformationProvider = new BuildInformationProvider();
	  final DynaTableWidget searchDynaTable = new DynaTableWidget(buildInformationProvider,
			  COLUMN_NAMES, STYLES, new boolean [] {true, true, true, true, true, true, true, true, true , true}, 15);
	  vpanel.add(searchDynaTable);
	  tabPanel.add(vpanel, "[ Search ]");
	  // Binaries panel
	  final DynaTableWidget binariesDynaTable = new DynaTableWidget(buildInformationProvider,
			  COLUMN_NAMES, STYLES, new boolean [] {false, false, true, true, false, false, false, false, false, false}, 15);
	  final BuildSummary binariesBuildSummary=new BuildSummary(buildInformationProvider, new BuildSummaryViewer() {
		  public String getSummary(Build build) {
			  return build.getBuildNumber();
		  }
	  });
	  vpanel=new VerticalPanel();
	  vpanel.add(createSummaryHeader("Build numbers:"));
	  vpanel.add(binariesBuildSummary);
	  vpanel.add(binariesDynaTable);
	  tabPanel.add(vpanel, "[ Binaries ]");
	  // Product releases panel
	  final DynaTableWidget releasesDynaTable = new DynaTableWidget(buildInformationProvider,
			  COLUMN_NAMES, STYLES, new boolean [] {true, true, true, true, false, false, false, false, false, false}, 15);
	  final BuildSummary productReleasesSummary=new BuildSummary(buildInformationProvider, new BuildSummaryViewer() {
		  public String getSummary(Build build) {
			  return "Release # "+build.getReleaseNumber();
		  }
	  });
	  vpanel=new VerticalPanel();
	  vpanel.add(createSummaryHeader("Release numbers:"));
	  vpanel.add(productReleasesSummary);
	  vpanel.add(releasesDynaTable);
	  tabPanel.add(vpanel, "[ Product releases ]");
	  // Source panel
	  final DynaTableWidget sourcesDynaTable1 = new DynaTableWidget(buildInformationProvider,
			  COLUMN_NAMES, STYLES, new boolean [] {false, false, false, false, true, true, true, true, true, true}, 15);
	  final DynaTableWidget sourcesDynaTable2 = new DynaTableWidget(buildInformationProvider,
			  COLUMN_NAMES, STYLES, new boolean [] {true, true, true, false, false, false, false, false, false, false}, 15);
	  final BuildSummary sourcesSummary1=new BuildSummary(buildInformationProvider, new BuildSummaryViewer() {
		  public String getSummary(Build build) {
			  return build.getCvsTag();
		  }
	  });
	  final BuildSummary sourcesSummary2=new BuildSummary(buildInformationProvider, new BuildSummaryViewer() {
		  public String getSummary(Build build) {
			  return "Version # "+build.getVersionNumber();
		  }
	  });
	  vpanel=new VerticalPanel();
	  vpanel.add(createSummaryHeader("CVS tags:"));
	  vpanel.add(sourcesSummary1);
	  vpanel.add(sourcesDynaTable1);
	  vpanel.add(createSummaryHeader("Version numbers:"));
	  vpanel.add(sourcesSummary2);
	  vpanel.add(sourcesDynaTable2);
	  tabPanel.add(vpanel, "[ Sources ]");
//	  tabPanel.add(new ConfigurationPanel(), "Configure");
	  tabPanel.setWidth("100%");
//	  tabPanel.setHeight("100%");
	  RootPanel.get("slot0").add(new Button("Refresh...", new ClickListener() {
		  public void onClick(Widget sender) {
//			  tabPanel.
			  buildInformationProvider.reload(new AsyncCallback () {
				 public void onFailure(Throwable caught) {
					DialogBox dialogBox = new DialogBox();
					dialogBox.setText("Failed to refresh...\n"+caught.getMessage());
					dialogBox.show();
				 }
				 public void onSuccess(Object result) {
					 // not optimal, but I haven't foudn another way....
					 binariesBuildSummary.refresh();
					 searchDynaTable.refresh();
					 binariesDynaTable.refresh();
					 releasesDynaTable.refresh();
					 sourcesDynaTable1.refresh();
					 sourcesDynaTable2.refresh();
				 }
			  });
		  }
	  }));
	  RootPanel.get("slot1").add(tabPanel);
	  tabPanel.selectTab(0);
  }
}
